/******************************************************************************** /
 * DictionaryModel.java - JPLearning Project									*
 * 																				*
 * 		This program is free software; you can redistribute it and/or modify	*
 *		it under the terms of the GNU General Public License as published by	*
 *		the Free Software Foundation; version 2 of the License.					*
 *																				*
 *		This program is distributed in the hope that it will be useful,			*
 *		but WITHOUT ANY WARRANTY; without even the implied warranty of			*
 *		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the			*
 *		GNU General Public License for more details.							*
 *																				*
 ********************************************************************************/
package org.jplearning.model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;

import org.jplearning.model.text.*;

public class DictionaryModel implements IDictionaryModel, IModelListener{
	private HashMap diccionario;
	private TreeMap inverso;
	
	private List listaListeners;
	
	public DictionaryModel(){
		diccionario = new HashMap();
		listaListeners = new LinkedList();
		inverso = new TreeMap();
	}
	
	public void addEntry(IText palabra, WordEntry entry){
		diccionario.put(palabra, entry);
		Iterator listaSignif = entry.getSignificados().iterator();
		while(listaSignif.hasNext()){
			String sign = (String)listaSignif.next();
			inverso.put(sign, palabra);
		}
		notifyEntryAdded(palabra);
	}
	
	public WordEntry getEntry(IText palabra){
		return (WordEntry)diccionario.get(palabra);
	}
	
	public IText getInverseEntry(String palabra){
		return (IText)inverso.get(palabra);
	}
	
	public void addModelListener(IModelListener listener){
		listaListeners.add(listener);
	}
	
	public void removeModelListener(IModelListener listener){
		listaListeners.remove(listener);
	}
	
	public void notifyEntryAdded(IText entry){
		Iterator itListeners = listaListeners.iterator();
		while(itListeners.hasNext()){
			((IModelListener)itListeners.next()).notifyEntryAdded(entry);
		}
	}

	/**
	 * 
	 */
	public HashMap getEntries() {
		return this.diccionario;
	}
}
